// subPages/health/recordWeight/recordWeight.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    line: '#50d2c2',
    styles: {
      line: '#d7d7d7',
      bginner: 'white',
      bgoutside: 'white',
      lineSelect: '',
      font: '#404040'
    },
  },

  //选中的体重
  bindvalue: function (e) {
    this.setData({
      value: e.detail.value,
    })
  },

  //腰围
  WCInput:function(e){
    this.setData({
      waist : e.detail.value
    })
  },

  //臀围
  HInput:function(e){
    this.setData({
      buttock : e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.weight && options.weight != 0 && options.weight != 'undefined'){
      that.setData({
        weight: options.weight,
        weightMax: options.weight * 2,
      })
    }else{
      that.setData({
        weight: 0,
        weightMax: 120
      })
    }
    var lineSelect = 'styles.lineSelect';
    that.setData({
      [lineSelect]: app.globalData.themeColor
    })
  },

    //查看历史详情
    recordSize:function(){
        var type = 2
        wx.navigateTo({
            url: '../recordSize/recordSize?type=' + type
        })
    },

  //完成记录
  save:function(){
    var that = this
    if(that.data.waist){
      let isWaist = util.validation(that.data.waist,'number')
      if(!isWaist){
        util.showModal('提示','腰围输入不正确')
        return;
      }
    }else{
      util.showModal('提示','请输入腰围')
      return;
    }
    if(that.data.buttock){
      let isButtock = util.validation(that.data.buttock,'number')
      if(!isButtock){
        util.showModal('提示','臀围输入不正确')
        return;
      }
    }else{
      util.showModal('提示','请输入臀围')
      return;
    }
    wx.request({
      url: app.globalData.apiUrl +'/add_weight', //接口地址
      data: {
        userid: app.globalData.userId,
        waist: that.data.waist,
        buttock: that.data.buttock,
        weight: that.data.value
      },
      method: 'post',
      header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.setData({
            waist: '',
            buttock: '',
          })
          util.showToast('保存成功','success')
        }else{
          util.showModal('提示','记录失败')
          return;
        }
      }
    })
  },

  //连接体脂秤
  connection: function () {
    var that = this
    wx.navigateTo({
      url: '../connection/connection?weight=' + that.data.weight
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN健康管理',
      path: 'pages/index/index',
      success: function(res) {
        // res.shareTickets
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})
